package com.zelin.gulimall.product.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zelin.common.utils.PageUtils;
import com.zelin.common.utils.Query;

import com.zelin.gulimall.product.dao.SkuInfoDao;
import com.zelin.gulimall.product.entity.SkuInfoEntity;
import com.zelin.gulimall.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        /**
         * key:
         * catelogId: 0
         * brandId: 0
         * min: 0
         * max: 0
         */
        if (params != null && params.size() > 0) {
            //1.得到参数
            String key = (String) params.get("key");
            String catelogId = (String) params.get("catelogId");
            String brandId = (String) params.get("brandId");
            String min = (String) params.get("min");
            String max = (String) params.get("max");
            //2.判断参数是否有值设置条件
            if (StringUtils.isNotBlank(key)) {
                wrapper.and(w -> {
                    w.eq("sku_id", key).or().like("sku_name", key);
                });
            }
            if (StringUtils.isNotBlank(catelogId) && !"0".equalsIgnoreCase(catelogId)) {
                wrapper.eq("catalog_id", catelogId);
            }
            if (StringUtils.isNotBlank(brandId) && !"0".equalsIgnoreCase(brandId)) {
                wrapper.eq("brand_id", brandId);
            }
            if (StringUtils.isNotBlank(min) && !"0".equalsIgnoreCase(min)) {
                wrapper.ge("price", min);
            }
            if (StringUtils.isNotBlank(max) && new Integer(max) > 0) {
                wrapper.le("price", max);
            }

        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

}